/**
 * 进制转换示例
*/

#include <stdio.h>
#include <string.h>
#include <math.h>

/**输出10进制数num对应的进制字面值*/
void feelTheBase(int num, int base)
{
    char num_str[33] = {'\0'};

    int temp = num;
    int i = 0;
    while (1)
    {
        int quotient = temp / base;
        int remainder = temp % base;

        num_str[i] = '0' + remainder;

        temp = quotient;
        if (quotient == 0)
        {
            break;
        }
        i++;
    }

    while (1)
    {
        printf("%c", num_str[i]);
        i--;
        if (0 > i)
        {
            break;
        }
    }
    printf("\n");
}

/**
 * 将对应进制数转换成进制
*/
void toDecimal(char *str_num, int base)
{
    int sum = 0;
    int len = strlen(str_num);
    for (int i = 0; i < len; ++i)
    {
        int temp = str_num[i] - 48;
        int weight = temp * ((int)pow(base, len - i - 1));
        sum += weight;
    }

    printf("%s to decimal %d\n", str_num, sum);
}

int main(void)
{

    feelTheBase(10, 2);
    feelTheBase(10, 8);
    feelTheBase(10, 10);

    printf("----------------------------------------------------\n");

    toDecimal("1100011", 2);
    toDecimal("143", 8);

    toDecimal("1010", 2);
    toDecimal("12", 8);
    return 0;
}